package com.younion.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.younion.common.constants.MsgStatus;
import com.younion.common.constants.Template;
import com.younion.vo.CostVO;
import com.younion.vo.CustomerVO;
import com.younion.vo.MemberCardCostCountVO;
import com.younion.vo.MemberCardVO;
import com.younion.vo.MessageVO;
import com.younion.vo.MsgTemplateVO;
import com.younion.vo.ProductVO;
import com.younion.vo.RegisterUserVO;
import com.younion.vo.SendMsgVO;

public class YounionUtil {
	
	
	public static String generateMessageContent(String content, MemberCardVO card){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		
		if(content.contains(Template.NAME.getTitleValiable()))
			content = content.replaceAll(Template.NAME.getTitleValiablePattern(), card.getMemberName());
		if(content.contains(Template.SEX.getTitleValiable())){
			String sex = (card.getMemberSex() == 0 ? "女士" : "先生");
			content = content.replaceAll(Template.SEX.getTitleValiablePattern(), sex);
		}
			
		if(content.contains(Template.COMPANY.getTitleValiable()))
			//TODO 你在｛公司｝的可用积分是说在发信息的商家的可用积分
			content = content.replaceAll(Template.COMPANY.getTitleValiablePattern(), card.getCustomerVO() == null ? "" : card.getCustomerVO().getName());
		if(content.contains(Template.SCORE.getTitleValiable()))
			content = content.replaceAll(Template.SCORE.getTitleValiablePattern(), card.getScore() == null ? "" : card.getScore().toString());
		if(content.contains(Template.GIFT.getTitleValiable()))
			content = content.replaceAll(Template.GIFT.getTitleValiablePattern(), card.getCardMeteringAmount() == null ? "" : card.getCardMeteringAmount().toString());
		if(content.contains(Template.MONEY.getTitleValiable()))
			content = content.replaceAll(Template.MONEY.getTitleValiablePattern(), card.getCardBalance() == null ? "" : card.getCardBalance().toString());
		if(content.contains(Template.TIME.getTitleValiable()))
			content = content.replaceAll(Template.TIME.getTitleValiablePattern(), sdf.format(new Date()));
		if(content.contains(Template.CUSTOMER.getTitleValiable()))
			content = content.replaceAll(Template.CUSTOMER.getTitleValiablePattern(), card.getCustomerVO() == null ? "" : card.getCustomerVO().getName());
		return content;
	}
	
	public static String generateMessageContentByDay(String content, Integer day){
		if(content.contains(Template.DAY.getTitleValiable()))
			content = content.replaceAll(Template.DAY.getTitleValiablePattern(), day.toString());
		return content;
	}
	
	public static String generateMessageContentByMembercardCostCount(String content, MemberCardCostCountVO costCount){
		content = YounionUtil.replaceTemplateByData(content,Template.PRODUCT,costCount.getProduct() == null ? "" : costCount.getProduct().getName());
		return content;
	}
	
	public static String generateMessageContentByProduct(String content, String productName){
		content = YounionUtil.replaceTemplateByData(content,Template.PRODUCT, productName == null ? "" : productName);
		return content;
	}
	
	public static String generateMessageContentByCost(String content, CostVO cost){
		content = YounionUtil.replaceTemplateByData(content,Template.COSTSCORE, cost.getScore() == null ? "" : cost.getScore().toString());
		return content;
	}
	
	public static String generateChangeData(String content, Integer addcount, Integer costcount, Integer addscore, Integer costscore,Integer count, Double addMoney, Double costMoney){
		content = YounionUtil.replaceTemplateByData(content,Template.ADDCOUNT, addcount == null ? "" : addcount.toString());
		content = YounionUtil.replaceTemplateByData(content,Template.COSTCOUNT, costcount == null ? "" : costcount.toString());
		content = YounionUtil.replaceTemplateByData(content,Template.ADDSCORE, addscore == null ? "" : addscore.toString());
		content = YounionUtil.replaceTemplateByData(content,Template.COSTSCORE, costscore == null ? "" : costscore.toString());
		content = YounionUtil.replaceTemplateByData(content,Template.COUNT, count == null ? "" : count.toString());
		content = YounionUtil.replaceTemplateByData(content,Template.ADDMONEY, addMoney == null ? "" : addMoney.toString());
		content = YounionUtil.replaceTemplateByData(content,Template.COSTMONEY, costMoney == null ? "" : costMoney.toString());
		return content;
	}
	
	
	public static String replaceTemplateByData(String content, Template template, String data){
		if(content.contains(template.getTitleValiable()))
			return content.replaceAll(template.getTitleValiablePattern(), data);
		return content;
	}
	
	public static SendMsgVO createSMS(Date sendTime, MsgTemplateVO template, MemberCardVO card, String content, RegisterUserVO user){
			if(user != null){
				SendMsgVO sms = new SendMsgVO();
				sms.setSendTime(sendTime);
				sms.setStatus(MsgStatus.UNSEND.getName());
				sms.setTemplate(template);
				sms.setUsers(card.getId().toString());
				sms.setContent(content);
				sms.setTitle(template.getTargetTypeTitle());
				sms.setCustomer(new CustomerVO(card.getCustomerId()));
				sms.setMobile(user.getLoginId());
				return sms;
			}
			return null;
	    }
	    
	public static MessageVO createMSG(Date sendTime, MsgTemplateVO template, MemberCardVO card, String content, RegisterUserVO user){
		if(user != null){
			MessageVO msg = new MessageVO();
	    	msg.setSendTime(sendTime);
	    	msg.setStatus(MsgStatus.UNSEND.getName());
	    	msg.setTemplate(template);
	    	msg.setUsers(card.getId().toString());
	    	msg.setContent(content);
	    	msg.setTitle(template.getTargetTypeTitle());
	    	msg.setCustomer(new CustomerVO(card.getCustomerId()));
	    	msg.setMobile(user.getLoginId());
    		msg.setUsertoken(user.getUserToken());
    		msg.setDeviceToken(user.getDeviceToken());
	    	return msg;
	    }
		return null;
}
}
